<?php
namespace Home\Controller;
use Think\Controller;
class UserController extends Controller {
    public function index(){
        $openid = I('openid');
        $name = I('name');
        $txsrc = I('txsrc');
        $result['state'] = 0;
        $user = M('user');
        $data = $user->where('openid="'.$openid.'"')->select();
        // $this->ajaxReturn($data[0]['bindstate'],'JSON');
        if(count($data) == 0){
            $user->openid = $openid;
            $user->name = $name;
            $user->txsrc = $txsrc;
            $user->bindstate = 0;
            $user->power = 1;
            $user->add();
            $this->ajaxReturn($result,'JSON');
        }
        else{
            if((int)$data[0]['bindstate'] != 1){
                $this->ajaxReturn($result,'JSON');
            }
            $result['state'] = 1;
            $result['power'] = (int)$data[0]['power'];
            $this->ajaxReturn($result,'JSON');
        }
    }

    public function getopenid(){
        $code = I('code');//小程序传来的code值
        $appid = 'wx0a5423110c05daa2';//小程序的appid
        $appSecret = 'cac65ce550f820a6225bd376f571209c';// 小程序的$appSecret
        $wxUrl = 'https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code';
        $getUrl = sprintf($wxUrl, $appid, $appSecret, $code);//把appid，appsecret，code拼接到url里

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $getUrl);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        //不做证书校验,部署在linux环境下请改为true
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
        $result = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);


        $wxResult = json_decode($result, true);
        if (empty($wxResult)) {
            echo '获取openid时异常，微信内部错误';
        } else {
            $loginFail = array_key_exists('errcode', $wxResult);
            if ($loginFail) {//请求失败
                var_dump($wxResult);
            } else {//请求成功
                $openid = $wxResult;
                $this->ajaxReturn($openid,'JSON');
            }
        }
    }

    public function binduser(){
        $save['staic'] = 0;
        $save['title'] = '情侣绑定失败！';
        $openid_A = I('openid_A');
        $openid_B = I('openid_B');
        $name = I('name');
        $txsrc = I('txsrc');
        $user = M('user');
        $data = $user->where('openid="'.$openid_A.'"')->select();
        if($data[0]['bindstate'] == 1 && $data[0]['openid_b'] != null){
            $save['title'] = '该用户已经有绑定情侣！';
            $this->ajaxReturn($save,'JSON');
        }
        $user->openid = $openid_A;
        $user->bindstate = 1;
        $user->openid_b = $openid_B;
        $user->name_b = $name;
        $user->txsrc_b = $txsrc;
        $resultsave = $user->save();
        $user->openid = $openid_B;
        $user->name = $name;
        $user->txsrc = $txsrc;
        $user->bindstate = 1;
        $user->openid_b = $openid_A;
        $user->name_b = $data[0]['name'];
        $user->txsrc_b = $data[0]['txsrc'];
        $user->power = 0;
        $resultadd = $user->add();
        if($resultsave && $resultadd){
            $save['staic'] = 1;
            $save['title'] = '情侣绑定成功！';
            $this->ajaxReturn($save,'JSON');
        }
        $this->ajaxReturn($save,'JSON');
    }
}